package com.imoresoft.agile.builder;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.ambitor.grass.sql.dao.SuperDao;
import com.ambitor.grass.util.data.DataMap;
import com.ambitor.grass.util.data.DatasetList;
import com.ambitor.grass.util.data.IData;
import com.ambitor.grass.util.data.IDataset;
import com.imoresoft.agile.exception.ProException;
import com.taobao.api.domain.Item;

public abstract class IModuleBuilder {

	private SuperDao superDao = null;

	private IData userData = null;

	private IData userStyle = null;

	private IData templetCode = new DataMap();

	private IData templetSkin = new DataMap();

	private String installPlace;

	private String title;

	protected final Logger logger = LoggerFactory.getLogger(this.getClass());

	public IModuleBuilder(SuperDao superDao, IData userData, IData userStyle,
			String installPlace, String title) throws Exception {
		this.superDao = superDao;
		this.userData = userData;
		this.userStyle = userStyle;
		this.installPlace = installPlace;
		this.title = title;
		init();
	}

	private void init() throws Exception {
		initTempletCode();
		initTempletSkin();
	}

	@SuppressWarnings("unchecked")
	private void initTempletCode() throws Exception {
		IData param = new DataMap();
		param.put("TEMPLET_ID", userStyle.getString("TEMPLET_ID"));
		IDataset dataset = new DatasetList();
		try {
			dataset = superDao.queryList("TD_A_TEMPLET_CODE",
					"templet_code_id,templet_code", param);
		} catch (Exception e) {
			logger.warn("查询TD_A_TEMPLET_CODE出错", e);
		}

		for (int i = 0; i < dataset.count(); i++) {
			IData data = dataset.getData(i);
			templetCode.put(data.getString("TEMPLET_CODE_ID").toUpperCase(),
					data.getString("TEMPLET_CODE"));
		}
	}

	@SuppressWarnings("unchecked")
	private void initTempletSkin() throws Exception {
		IData param = new DataMap();
		param.put("TEMPLET_ID", userStyle.getString("TEMPLET_ID"));
		param.put("SKIN_ID", userStyle.getString("SKIN_ID"));
		IDataset dataset = new DatasetList();
		try {
			dataset = superDao.queryList("TD_A_TEMPLET_SKIN", "attr,value",
					param);
		} catch (Exception e) {
			logger.warn("查询TD_A_TEMPLET_SKIN出错", e);
		}

		for (int i = 0; i < dataset.count(); i++) {
			IData data = dataset.getData(i);
			templetSkin.put(data.getString("ATTR").toUpperCase(), data
					.getString("VALUE"));
		}
	}

	public SuperDao getSuperDao() {
		return superDao;
	}

	public void setSuperDao(SuperDao superDao) {
		this.superDao = superDao;
	}

	public IData getUserSession() {
		return userData;
	}

	public void setUserSession(IData userSession) {
		this.userData = userSession;
	}

	public IData getUserStyle() {
		return userStyle;
	}

	public void setUserStyle(IData userStyle) {
		this.userStyle = userStyle;
	}

	public IData getUserData() {
		return userData;
	}

	public void setUserData(IData userData) {
		this.userData = userData;
	}

	public IData getTempletCode() {
		return templetCode;
	}

	public void setTempletCode(IData templetCode) {
		this.templetCode = templetCode;
	}

	public IData getTempletSkin() {
		return templetSkin;
	}

	public void setTempletSkin(IData templetSkin) {
		this.templetSkin = templetSkin;
	}

	public String getInstallPlace() {
		return installPlace;
	}

	public void setInstallPlace(String installPlace) {
		this.installPlace = installPlace;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public abstract String buildPromHTML(List<Item> allItemListToBePromoted)
			throws ProException;
}